layui.config({
    base : "js/"
}).use(['form','layer','jquery','laypage','table','laytpl','laydate'],function(){
    var form = layui.form,table = layui.table;
    layer = parent.layer === undefined ? layui.layer : parent.layer,
        laypage = layui.laypage,
        laydate = layui.laydate,
        nowTime = new Date().valueOf(),
        max = null,
        $ = layui.jquery;

    $('.search_btn').on('click', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    //监听表格复选框选择
    table.on('checkbox(list)', function(obj){
        console.log(obj)
    });
    //监听工具条
    table.on('tool(list)', function(obj){
        var data = obj.data;
        if(obj.event === 'show'){
            layer.alert('查看行：<br>'+ JSON.stringify(data))
        } else if(obj.event === 'del'){
            layer.confirm('真的删除行么', function(index){
                $.ajax({
                    url:ctx+'/member/delById/'+data.id,
                    type : "get",
                    success : function(d){
                        if(d.code==0){
                            //obj.del();
                            table.reload('listTable', {})
                        }else{
                            layer.msg("权限不足，联系超管！",{icon: 5});
                        }
                    }
                })
                layer.close(index);

            });
        } else if(obj.event === 'edit'){
            /* 弹窗和子窗口风格二选一//layer.alert('编辑行：<br>'+ JSON.stringify(data)) */
            //   layer.open({
            //       type: 2,
            //       title:"编辑信用卡信息",
            //       area: ['680px', '560px'],
            //       content:ctx+"/xinyongka/editXYK/"+data.id //这里content是一个普通的String
            //   })
            var index = layui.layer.open({
                title : "编辑会员信息",
                type : 2,
                content : ctx+"/member/editUser/"+data.id, //这里content是一个普通的String
                success : function(layero, index){
                    layui.layer.tips('点击此处返回信用卡列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                }
            })
            //改变窗口大小时，重置弹窗的高度，防止超出可视区域（如F12调出debug的操作）
            $(window).resize(function(){
                layui.layer.full(index);
            })
            layui.layer.full(index);
        }
    });

    var $ = layui.$, active = {
        getCheckData: function(){ //获取选中数据
            var checkStatus = table.checkStatus('listTable')
                ,data = checkStatus.data;
            if(data.length<1){
                layer.alert('请选择数据')
            }else{
                var ids ="";
                for(var i=0;i<data.length;i++){
                    ids+="ids="+data[i].id+"&";
                }
                layer.alert('选中ID：<br>'+ids)
            }
        }
    };
    $('.listTable .layui-btn').on('click', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });
    var $ = layui.$, active = {
        search: function(){ //获取
            var nickname = $('#nickname'), member = $('#member option:selected'), status = $('#status option:selected');
            createTimeStart = $("#createTimeStart"),
                createTimeEnd = $("#createTimeEnd");
            //执行重载
            table.reload(
                'listTable',
                {
                    page : {
                        curr : 1
                        //重新从第 1 页开始
                    },
                    where : {
                        //key:
                        nickname : nickname.val(),
                        member : member.val(),
                        status : status.val(),
                        createTimeStart : createTimeStart.val(),
                        createTimeEnd : createTimeEnd.val()

                    }
                });
        }
    };

        var start = laydate.render({
            elem: '#createTimeStart',
            type: 'datetime',
            max: nowTime,
            btns: ['clear', 'confirm'],
            done: function (value, date) {
                endMax = end.config.max;
                end.config.min = date;
                end.config.min.month = date.month - 1;
            }
        });
        var end = laydate.render({
            elem: '#createTimeEnd',
            type: 'datetime',
            max: nowTime,
            done: function (value, date) {
                if ($.trim(value) == '') {
                    var curDate = new Date();
                    date = {
                        'date': curDate.getDate(),
                        'month': curDate.getMonth() + 1,
                        'year': curDate.getFullYear()
                    };
                }
                start.config.max = date;
                start.config.max.month = date.month - 1;
            }
        })
   

    $("#testdata").click(function(){
        $.ajax({
            type: "post",
            url: ctx+"/member/list",
            data: {"page":1,"limit":10},
            dataType: 'json',
            success: function(r){
                var last=JSON.stringify(r);
                alert(last);

            }
            ,error: function(e, m){
                alert("数据接口请求异常");

            }
        });
    });

//添加
    $(".userAdd_btn").click(function(){
        var index = layui.layer.open({
            title : "添加会员",
            type : 2,
            content : ctx+"/member/addUser",
            success : function(layero, index){
                layui.layer.tips('点击此处返回会员列表', '.layui-layer-setwin .layui-layer-close', {
                    tips: 3
                });
            }
        })
        //改变窗口大小时，重置弹窗的高度，防止超出可视区域（如F12调出debug的操作）
        $(window).resize(function(){
            layui.layer.full(index);
        })
        layui.layer.full(index);
    })
    //批量删除
    $(".batchDel").click(function(){
        var checkStatus = table.checkStatus('listTable')
            ,data = checkStatus.data,xykStr='',flag=false;
//	      layer.alert(JSON.stringify(data));
        if(data.length>0){
            $.each(data, function (n, value) {
                xykStr+=value.id+',';
            });
            xykStr=xykStr.substring(0,xykStr.length-1);
            layer.confirm('真的要删除<strong>'+data.length+'</strong>条数据吗？', function(index){
                //调用删除接口
                $.ajax({
                    url:ctx+'/member/delPatch/'+xykStr,//接口地址
                    type : "get",
                    success : function(d){
                        if(d.code==0){
                            //删除成功，刷新父页面
                            parent.location.reload();
                        }else{
                            layer.msg("删除错误，稍后再试！",{icon: 5});
                        }
                    }
                })
            });
        }else{
            layer.msg("请选择要操作的数据！");
        }

    })

});
